package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Permission;
import com.service.PermissionService;
import com.util.ConstUtils;
import com.util.TreeNode;
import com.vo.DataVO;
import com.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/19 - 10:10
 */
@RestController
@RequestMapping("/permission")
@RolesAllowed({"ROLE_ADMIN", "ROLE_SUPERADMIN"}) //JSR250权限注解 或者@Secured注解
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    //加载权限页面左侧树形菜单
    @RequestMapping("/loadMenuTree")
    public DataVO loadMenuTree() {
        //查询权限集合
        List<Permission> permissionList = permissionService.findPermission(null);
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        for (Permission permission : permissionList) {
            //如果该权限的spread为1则页面中的该节点为展开状态
            boolean spread = permission.getSpread() == 1 ? true : false;
            treeNodes.add(new TreeNode(permission.getId(), permission.getPid(), permission.getTitle(), spread));
        }
        return new DataVO<TreeNode>(null, treeNodes);
    }

    //加载权限页面右侧表格
    @RequestMapping("/list")
    public DataVO getPermissionList(PermissionVo permissionVo) {
        PageHelper.startPage(permissionVo.getPage(), permissionVo.getLimit());
        PageInfo<Permission> pageInfo = new PageInfo<Permission>(permissionService.findPermission(permissionVo));
        return new DataVO<Permission>(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/add")
    public Map<String, Object> addPermission(Permission permission) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (permissionService.addPermission(permission) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    @RequestMapping("/update")
    public Map<String, Object> updatePermission(Permission permission) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (permissionService.updatePermission(permission) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    //删除菜单信息
    @RequestMapping("/del")
    public Map<String, Object> deletePermission(Integer permissionId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (permissionService.deletePermission(permissionId)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }
}
